/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: Nvm.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: Nvm
*
*******************************************************************************/

/** HEADER FILES **/
#include "Nvm.h"
#include "NvmJob.h"


/** MACRO DEFINITION **/


/** TYPEDEF DEFINITION **/


/** LOCAL DATA DECLARATION **/


/** GLOBAL DATA DECLARATION **/


/** LOCAL FUNCTION DECLARATION **/


/** GLOBAL FUNCTION DECLARATION **/
void Nvm_GetVersionInfo(
    Std_VersionInfoType* versionInfo
);

Std_ReturnType NvM_ReadBlock(
    NvM_BlockIdType BlockId,
    void* NvM_DstPtr
);

Std_ReturnType NvM_WriteBlock(
    NvM_BlockIdType BlockId,
    const void* NvM_SrcPtr
);

/** LOCAL DATA **/
NvM_BlockManagementType NvmAdministrativeBlock[NvmConf_Block_Max];


/** GLOBAL DATA **/
NvM_QueueType* CurrentQueue;

/** LOCAL FUNCTION **/


/** GLOBAL FUNCTION **/

Std_ReturnType NvM_GetErrorStatus
(
    NvM_BlockIdType BlockId,
    NvM_RequestResultType* RequestResultPtr
);

Std_ReturnType NvM_ReadBlock(
    NvM_BlockIdType BlockId,
    void* NvM_DstPtr
);

Std_ReturnType NvM_WriteBlock(
    NvM_BlockIdType BlockId,
    const void* NvM_SrcPtr
);

void NvM_ReadAll(
    void
);

void Nvm_GetVersionInfo(
    Std_VersionInfoType* versionInfo
);

void NvM_Init(
    const NvM_ConfigType* ConfigPtr
);

void NvM_MainFunction(
    void
);


/** GLOBAL FUNCTION **/

/*******************************************************************************
* -Name            Nvm_GetVersionInfo
* -Brief           Service to read the block dependent error/status information
* -Details         None
* -Param[in]       The block identifier uniquely identifies one NVRAM block descriptor. A NVRAM block descriptor contains all needed
*                  information about a single NVRAM block
* -Param[out]      RequestResultPtr:  Pointer to where to store the request result. See NvM_Request ResultType
* -Return          E_OK: The block dependent error/status information was readsuccessfully.
                   E_NOT_OK: An error occured          
*******************************************************************************/
Std_ReturnType NvM_GetErrorStatus
(
    NvM_BlockIdType BlockId,
    NvM_RequestResultType* RequestResultPtr
)
{
    Std_ReturnType retVal = E_NOT_OK;
    NvM_BlockManagementType* nvmBlockManagementPtr = (NvM_BlockManagementType*)&NvmAdministrativeBlock[BlockId];

    *RequestResultPtr = nvmBlockManagementPtr->RequestStatus;

    return(retVal);
}


/*******************************************************************************
* -Name            NvM_ReadBlock
* -Brief           Service to copy the data of the NV block to its corresponding RAM block 
* -Details         None
* -Param[in]       BlockId: The block identifier uniquely identifies one NVRAM block descriptor
* -Param[out]      NvM_DstPtr: Pointer to the RAM data block
* -Return          None          
*******************************************************************************/
Std_ReturnType NvM_ReadBlock(
    NvM_BlockIdType BlockId,
    void* NvM_DstPtr
)
{
    Std_ReturnType retVal = E_NOT_OK;
    NvM_BlockManagementType* nvmBlockManagementPtr = (NvM_BlockManagementType*)&NvmAdministrativeBlock[BlockId];

    retVal = NvM_QueueJob(BlockId, NVM_JOB_ID_READ_BLOCK, NvM_DstPtr);
    nvmBlockManagementPtr->RequestStatus = NVM_REQ_PENDING;

    return(retVal);
}


/*******************************************************************************
* -Name            NvM_WriteBlock
* -Brief           Service to copy the data of the RAM block to its corresponding NV block 
* -Details         None
* -Param[in]       BlockId: The block identifier uniquely identifies one NVRAM block descriptor
* -Param[in]       NvM_DstPtr: Pointer to the RAM data block
* -Return          None          
*******************************************************************************/
Std_ReturnType NvM_WriteBlock(
    NvM_BlockIdType BlockId,
    const void* NvM_SrcPtr
)
{
    Std_ReturnType retVal = E_NOT_OK;
    
    NvM_QueueJob(BlockId, NVM_JOB_ID_WRITE_BLOCK, NvM_SrcPtr);

    return(retVal);
}

/*******************************************************************************
* -Name            NvM_SetRamBlockStatus
* -Brief           ServiceforsettingtheRAMblockstatusofapermanentRAMblockorthestatusoftheexplicit synchronizationofaNVRAMblock 
* -Details         None
* -Param[in]       BlockId:  TheblockidentifieruniquelyidentifiesoneNVRAMblock
*                            descriptor.ANVRAMblockdescriptorcontainsallneeded
*                            informationaboutasingleNVRAMblock
* -Param[in]       BlockChanged: TRUE:ValidatethepermanentRAMblockortheexplicit
*                                synchronizationandmarkblockaschanged.FALSE:Invalidate
*                                thepermanentRAMblockortheexplicitsynchronizationand
*                                markblockasunchanged
* -Return          E_OK:ThestatusofthepermanentRAMblockortheexplicit synchronizationwaschangedasrequested.
*                  E_NOT_OK:Anerroroccurred.          
*******************************************************************************/
Std_ReturnType NvM_SetRamBlockStatus(
    NvM_BlockIdType BlockId,
    boolean BlockChanged
)
{
    Std_ReturnType retVal = E_OK;
    NvM_BlockManagementType* nvmBlockManagementPtr = (NvM_BlockManagementType*)&NvmAdministrativeBlock[BlockId];

    nvmBlockManagementPtr->BlockChanged = BlockChanged;

    return(retVal);
}

/*******************************************************************************
* -Name            NvM_ReadAll
* -Brief           Initiates a multi block read request 
* -Details         None
* -Param[in]       None
* -Return          None          
*******************************************************************************/
void NvM_ReadAll(
    void
)
{
    NvMJob_ReadAll();
}


/*******************************************************************************
* -Name            NvM_WriteAll
* -Brief           Initiates a multi block write request 
* -Details         None
* -Param[in]       None
* -Return          None          
*******************************************************************************/
void NvM_WriteAll(
    void
)
{
    NvMJob_WriteAll();
}

/*******************************************************************************
* -Name            Nvm_GetVersionInfo
* -Brief           Returns the version information of this module 
* -Details         None
* -Param[out]      versioninfo: Pointer to the version information of this module
* -Return          None          
*******************************************************************************/
void Nvm_GetVersionInfo(
    Std_VersionInfoType* versionInfo
)
{
    versionInfo->vendorID = (uint16)YSAR_VENDOR_ID;
    versionInfo->moduleID = (uint8)NVM_MODULE_ID;
    versionInfo->sw_major_version = (uint8)NVM_SW_MAJOR_VERSION;
    versionInfo->sw_minor_version = (uint8)NVM_SW_MINOR_VERSION;
    versionInfo->sw_patch_version = (uint8)NVM_SW_PATCH_VERSION;
}



/*******************************************************************************
* -Name            NvM_Init
* -Brief           Service for resetting all internal variables 
* -Details         None
* -Param[in]       Pointer to the selected configuration set
* -Return          None          
*******************************************************************************/
void NvM_Init(
    const NvM_ConfigType* ConfigPtr
)
{
    NvM_ReadAll();
    NvM_QueueInit();
}

/*******************************************************************************
* -Name            NvM_MainFunction
* -Brief           Service for performing the processing of the NvM jobs 
* -Details         None
* -Return          None          
*******************************************************************************/
void NvM_MainFunction(
    void
)
{
    NvM_BlockManagementType* nvmBlockManagementPtr;
    static NvM_QueueType queue;

    if(E_OK == NvM_QueuePop((NvM_QueueType*)&queue))
    {
        NvM_JobFctTable[queue.JobId]();
        nvmBlockManagementPtr = (NvM_BlockManagementType*)&NvmAdministrativeBlock[queue.BlockId];
        nvmBlockManagementPtr->RequestStatus = NVM_REQ_OK;
    }
}


